#include "msgproto.h"

#include <QTextCodec>

msgproto::msgproto()
{
    memcpy(devsn, "sn123456789012", 14);
}

bool msgproto::in(char *buf, int size, int &cmd)
{
    proto_hdr_t *hdr;
    bool ret = false;

    hdr = (proto_hdr_t*)buf;

    if (hdr->sflag1 == 0x68 && hdr->sflag2 == 0x68)
    {
        cmd = hdr->cmd;
        memcpy(devsn, hdr->sn, 14);
        ret = true;
    }

    return ret;
}

bool msgproto::makeheartbeat(char *out, int &size)
{
    proto_hdr_t *hdr;

    hdr = (proto_hdr_t *)out;
    proto_head_init(hdr, 0xA1, 0);
    size = sizeof(proto_hdr_t);
    proto_end((uint8_t*)out, size);

    return true;
}

bool msgproto::makelogin(char *out, int &size)
{
    devlogin_t *lg;

    lg = (devlogin_t *)out;
    lg->status = 0;
    proto_head_init(&lg->hdr, 0xAF, 1);
    size = sizeof(devlogin_t);
    proto_end((uint8_t*)out, size);

    return true;
}

void msgproto::makeprint(string &text, char *out, int &size)
{
    textprint_t *pt;
    int len;

    pt = (textprint_t*)out;
    len = text.size();
    proto_head_init(&pt->hdr, 0x08, len + 8);
    pt->msgid = 1;
    pt->size = len;
    memcpy(pt->data, text.c_str(), len);
    size = len + sizeof(textprint_t) - sizeof(pt->data);

    proto_end((uint8_t*)out, size);
}

uint8_t msgproto::chksum(uint8_t *data, int size)
{
    uint8_t sum = 0;

    for (int i = 0; i < size; i ++)
    {
        sum += data[i];
    }

    return sum;
}

void msgproto::proto_head_init(proto_hdr_t *hdr, unsigned char cmd, short size)
{
    memcpy(hdr->sn, devsn, 14);
    hdr->cmd = cmd;
    hdr->size = size + 3;
    hdr->sflag1 = 0x68;
    hdr->sflag2 = 0x68;
}

void msgproto::proto_end(uint8_t *data, int &size)
{
    data[size] = chksum(data, size);
    size ++;
    data[size++] = 0x16;
}

QByteArray msgproto::makePrintItemText(QString s, int x, int y, int fs)
{
    QByteArray item, t;

    t = utf8ToGb2312(s);

    item = makePrintItem(0, x, y, fs, 0, t);

    return item;
}

QByteArray msgproto::makePrintItemQr(QString s, int x, int y, int fs)
{
    QByteArray item, t;

    t = utf8ToGb2312(s);

    item = makePrintItem(0, x, y, fs, 0, t);

    return item;
}

QByteArray msgproto::utf8ToGb2312(QString &strUtf8)
{
    QTextCodec* utf8Codec= QTextCodec::codecForName("utf-8");
    QTextCodec* gb2312Codec = QTextCodec::codecForName("gb2312");

    QString strUnicode= utf8Codec->toUnicode(strUtf8.toStdString().c_str());
    QByteArray ByteGb2312= gb2312Codec->fromUnicode(strUnicode);

    ByteGb2312.append('\0');

    return ByteGb2312;
}

QByteArray msgproto::makePrintItem(int type, int x, int y, int p1, int p2, QByteArray text)
{
    QByteArray hdr;
    sprint_item_t *item;

    hdr.resize(sizeof(sprint_item_t));
    item = (sprint_item_t *)hdr.data();
    item->type = type;
    item->x = x;
    item->y = y;
    item->parm1 = p1;
    item->parm2 = p2;
    item->tlen = text.size();

    hdr += text;
    hdr.append(0xad);
    hdr.append(0xed);

    return hdr;
}

QByteArray msgproto::makeServerPrint(int num, QByteArray &data)
{
    QByteArray buf;
    sprint_req_t *pt;
    int len;
    uint8_t sum;

    buf.resize(sizeof(sprint_req_t));
    pt = (sprint_req_t *)buf.data();
    len = data.size() + 1;
    proto_head_init(&pt->hdr, 0x08, len);
    pt->num = num;
    buf += data;
    sum = chksum((uint8_t*)buf.data(), buf.size());
    buf.append(sum);
    buf.append(0x16);

    return buf;
}
